home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 6
/
FM Towns Free Software Collection 6.iso
/
t_os
/
et
/
src
/
file.c
< prev
next >
Wrap
Text File
|
1993-07-08
|
12KB
|
449 lines
#include "et.h"
int save_key(int file_i)/*et*/
{
FILE *File;
int i, mm, key_n;
char *c_p, *demo_p;
char *file_p;
unsigned int f;
key_n = nb_key(G_mv);
if (test_bit(G_f, f_DEMO)) {
if (file_i < 0)
return(1);
G_bf.key_icn[G_nb.key+1] = G_pn.key_fp;
_far_copy_buf(0x14,P_v[v_ICN], 0x120, G_pn.key_fp, P_i[i_SIZE_ICN][0]);
G_pn.key_fp += P_i[i_SIZE_ICN][0];
if (G_nb.key == 7) /* 未使用 VRAM が足りないので切り詰め(^^; */
G_bf.key[8] = G_pn.fp = G_bf.key[1];
else
G_bf.key[G_nb.key+1] = G_pn.fp; /* for load_key() */
c_p = P_c[c_CSR];
do {
_poke_byte(0x120, G_pn.fp++, *c_p);
} while (*c_p++ != EOC);
make_idx(file_i);
switch(G_nb.key) { /* 切り詰め */
case 2:
case 3:
case 4:
case 6:
case 8:
break;
default:
_poke_longword(0x120, G_pn.fp, key_n);
G_pn.fp += sizeof(int);
_far_copy_buf(0x14, G_mv, 0x120, G_pn.fp, sizeof(MOVE)*(key_n+1));
G_pn.fp += sizeof(MOVE)*(key_n+1);
_far_copy_buf(0x14, &G_pm, 0x120, G_pn.fp, sizeof(PARAMETER));
G_pn.fp += sizeof(PARAMETER);
f = test_bit(G_f, (f_AA|f_NN));
_far_copy_buf(0x14, &f, 0x120, G_pn.fp, sizeof(G_f));
G_pn.fp += sizeof(G_f);
break;
}
return(1);
}
mm = file_i;
if (file_i < 0) {
file_i = 0;
file_p = P_c[c_FILE_BAK];
} else
file_p = P_c[c_FILE_KEY];
set_file_i(&file_p[8], file_i);
File = et_fopen(file_p, "wb", NULL);
G_nb.drive = *file_p;
et_fwrite(P_v[v_ICN], 1, P_i[i_SIZE_ICN][0], File);
c_p = P_c[c_CSR];
do {
fwrite(c_p, 1, 1, File);
} while (*c_p++ != EOC);
if (file_i)
make_idx(file_i);
et_fwrite(&key_n, sizeof(int), 1, File);
et_fwrite(G_mv, sizeof(MOVE), key_n+1, File);
et_fwrite(&G_pm, sizeof(PARAMETER), 1, File);
f = test_bit(G_f, f_AA|f_NN|f_RR);
et_fwrite(&f, sizeof(G_f), 1, File);
et_fwrite(&G_nb.key_i, sizeof(int), 1, File);
fclose(File);
if (mm < 0)
return(1);
File = et_fopen(P_c[c_FILE_ICN_KEY], "ab", NULL);
G_nb.drive = *P_c[c_FILE_ICN_KEY];
fseek(File, P_i[i_SIZE_ICN][0] * (file_i-1), 0);
et_fwrite(P_v[v_ICN], 1, P_i[i_SIZE_ICN][0], File);
fclose(File);
return(1);
}
int make_icn(int icn_i, int file_i)/*et*/
{
FILE *File, *Temp;
int i;
char *c_p;
char file_a[20];
if (test_bit(G_f, f_DEMO))
return;
Temp = et_fopen(P_c[icn_i], "wb", NULL);
G_nb.drive = *P_c[icn_i];
i = 0;
while (1) {
set_file_i(&P_c[file_i][8], i+1);
strcpy(file_a, P_c[file_i]);
File = fopen(file_a, "rb");
if (!File)
break;
copy_file(Temp, File, (int)P_i[i_SIZE_ICN][0]);
if (file_i == c_FILE_KEY) {
c_p = P_c[c_CSR];
do {
*c_p = fgetc(File);
} while (*c_p++ != EOC);
make_idx(i);
}
fclose(File);
i++;
}
fclose(Temp);
return(i);
}
void load_et(int i_et,int mm)/*et*/
{
FILE *File;
int i, j;
int *p;
char *demo_p, file_a[20];
int max_i;
max_i = G_nb.edge-G_nb.max_edge;
if (!i_et) {
G_nb.edge = nb_EDGE;
p = P_i[i_CSR_000_ET];
for (i = 0; i < G_nb.edge; i++)
for (j = 0; j < 2; j++) {
G_et[i].xyz[j] = *p++;
G_et[i].xyz[j+2] = *p++;
G_et[i].xyz[j+4] = *p++;
}
} else
if (test_bit(G_f, f_DEMO)) {
demo_p = G_bf.csr[i_et];
G_nb.edge = _peek_longword(0x120, demo_p);
demo_p += sizeof(G_nb.edge);
_far_copy_buf(0x120, demo_p, 0x14, G_et, sizeof(ET)*G_nb.edge);
} else {
strcpy(file_a, P_c[c_FILE_CSR]);
set_file_i(&file_a[8], i_et);
File = fopen(file_a, "rb");
if (!test_bit(G_f, f_DEMO|f_MAKE) && !File) {/* 新しいファイルがない */
file_a[9] = '_';
File = et_fopen(file_a, "rb", NULL);
}
fseek(File, (long)P_i[i_SIZE_ICN][0], 0);
fread(&G_nb.edge, sizeof(int), 1, File);
if (G_nb.edge > G_nb.max_edge) {
fread(G_et, sizeof(ET), G_nb.max_edge, File);
fread(G_bf.et_buf, sizeof(ET), max_i, File);
G_nb.edge = G_nb.max_edge;
} else
fread(G_et, sizeof(ET), G_nb.edge, File);
if (!mm)
fread(&G_pm, sizeof(PARAMETER), 1, File);
fclose(File);
}
}
int load_key(int file_i, int mm)/*et*/
{
FILE *File, *Temp;
int i, j, key_i, csr_i, key_n, ins_n;
unsigned int f;
char *csr_p;
char *demo_p, file_a[20];
if (!file_i) {
G_mv[0].key = EOK;
P_c[c_CSR][0] = 0;
P_c[c_CSR][1] = EOC;
init_pm();
G_nb.key_i = G_nb.et = 0;
res_bit(G_f, f_AA|f_NN|f_RR);
} else {
if (mm) {
key_i = nb_key(G_mv);
csr_i = nb_csr(P_c[c_CSR]);
ins_n = instring(S_RET, P_i[i_KEY_MOVE], sizeof(int));
} else {
key_i = 0;
csr_p = P_c[c_CSR];
ins_n = EOK+1;
}
if (test_bit(G_f, f_DEMO)) {
demo_p = G_bf.key[file_i];
if (mm)
while (_peek_byte(0x120, demo_p++) != EOC)
;
else
do {
*csr_p = _peek_byte(0x120, demo_p++);
} while (*csr_p++ != EOC);
key_n = _peek_longword(0x120, demo_p);
demo_p += sizeof(int);
if (key_n + key_i > MAX_KEY)
key_n = MAX_KEY - key_i;
_far_copy_buf(0x120, demo_p, 0x14, &G_mv[key_i],sizeof(MOVE)*(key_n+1));
demo_p += sizeof(MOVE)*(key_n+1);
G_mv[key_i+key_n].key = EOK;
if (mm)
return(1);
_far_copy_buf(0x120, demo_p, 0x14, &G_pm, sizeof(PARAMETER));
demo_p += sizeof(PARAMETER);
_far_copy_buf(0x120, demo_p, 0x14, &f, sizeof(G_f));
res_bit(G_f, f_AA|f_NN);
set_bit(G_f, f);
return(1);
}
if (file_i < 0) {
file_i = 0;
strcpy(file_a, P_c[c_FILE_BAK]);
} else
strcpy(file_a, P_c[c_FILE_KEY]);
set_file_i(&file_a[8], file_i);
File = fopen(file_a, "rb");
if (!test_bit(G_f, f_DEMO|f_MAKE) && !File) {/* 新しいファイルがない */
file_a[9] = '_';
File = et_fopen(file_a, "rb", NULL);
}
fseek(File, (long)P_i[i_SIZE_ICN][0], 0);
if (mm)
while (fgetc(File) != EOC)
;
else
do {
*csr_p = fgetc(File);
} while (*csr_p++ != EOC);
fread(&key_n, sizeof(int), 1, File);
if (key_n + key_i > MAX_KEY)
key_n = MAX_KEY - key_i;
fread(&G_mv[key_i], sizeof(MOVE), (key_n+1), File);
G_mv[key_i+key_n].key = EOK;
if (mm) {
fclose(File);
return(1);
}
fread(&G_pm, sizeof(PARAMETER), 1, File);
fread(&f, sizeof(G_f), 1, File);
res_bit(G_f, f_AA|f_NN|f_RR);
set_bit(G_f, f);
fread(&G_nb.key_i, sizeof(int), 1, File);
fclose(File);
}
return(1);
}
void ren_file(char file_a[], int dst_i, int src_i)/*et*/
{
int i;
char dst_a[20], src_a[20];
FILE *File;
if (!_disk_free(1))
return;
i = 1;
while (1) {
set_file_i(&file_a[8], i);
strcpy(dst_a, file_a);
dst_a[9] = dst_i;
strcpy(src_a, file_a);
src_a[9] = src_i;
File = fopen(src_a, "rb");
if (File) {
fclose(File);
unlink(dst_a);
rename(src_a, dst_a);
} else {
File = fopen(dst_a, "rb");
if (File)
fclose(File);
else
break;
}
i++;
}
}
int kill_file(int mm)/*et*/
{
int i;
switch(mm) {
case 1:
for (i = 0; i <= G_nb.key; i++) {
set_file_i(&P_c[c_FILE_KEY][8], i);
unlink(P_c[c_FILE_KEY]);
}
for (i = 0; i <= G_nb.csr; i++) {
set_file_i(&P_c[c_FILE_CSR][8], i);
unlink(P_c[c_FILE_CSR]);
}
case 3:
unlink(P_c[c_FILE_ICN_KEY]);
unlink(P_c[c_FILE_ICN_CSR]);
unlink(P_c[c_FILE_ET_TEMP]);
case 2:
for (i = 1; i <= 5; i++) {
set_file_i(&P_c[c_FILE_BAK][8], i);
unlink(P_c[c_FILE_BAK]);
}
break;
}
}
void copy_file(FILE *File_d, FILE *File_s, int n)/*et*/
{
int i; /* G_bf.et_buf をバッファに使用 */
i = (int)G_nb.max_edge*sizeof(ET);
if (!n)
return;
if (n <= i) {
fread(G_bf.et_buf, 1, n, File_s);
et_fwrite(G_bf.et_buf, 1, n, File_d);
} else {
fread(G_bf.et_buf, 1, i, File_s);
et_fwrite(G_bf.et_buf, 1, i, File_d);
n -= i;
copy_file(File_d, File_s, n);
}
}
void save_et(int edge_i, int file_i)/*et*/
{
FILE *File;
char *file_p;
file_p = P_c[c_FILE_CSR];
if (test_bit(G_f, f_DEMO)) {
G_bf.csr_icn[G_nb.csr+1] = G_pn.csr_fp;
_far_copy_buf(0x14, P_v[v_ICN], 0x120, G_pn.csr_fp,P_i[i_SIZE_ICN][0]);
G_pn.csr_fp += P_i[i_SIZE_ICN][0];
G_bf.csr[G_nb.csr+1] = G_pn.fp;
_poke_longword(0x120, G_pn.fp, edge_i);
G_pn.fp += sizeof(edge_i);
_far_copy_buf(0x14, G_bf.et_buf, 0x120, G_pn.fp, sizeof(ET)*edge_i);
G_pn.fp += sizeof(ET)*edge_i;
_far_copy_buf(0x14, &G_pm, 0x120, G_pn.fp, sizeof(PARAMETER));
G_pn.fp += sizeof(PARAMETER);
return;
}
set_file_i(&file_p[8], file_i);
File = et_fopen(file_p, "wb", NULL);
G_nb.drive = *file_p;
et_fwrite(P_v[v_ICN], 1, P_i[i_SIZE_ICN][0], File);
et_fwrite(&edge_i, sizeof(int), 1, File);
et_fwrite(G_bf.et_buf, sizeof(ET), edge_i, File);
et_fwrite(&G_pm, 1, sizeof(PARAMETER), File);
fclose(File);
File = et_fopen(P_c[c_FILE_ICN_CSR], "ab", NULL);
G_nb.drive = *P_c[c_FILE_ICN_CSR];
fseek(File, P_i[i_SIZE_ICN][0] * (file_i-1), 0);
et_fwrite(P_v[v_ICN], 1, P_i[i_SIZE_ICN][0], File);
fclose(File);
}
FILE *et_fopen(char file_a[], char mm_a[], FILE *Temp)/*et*/
{
FILE *File;
int menu_i;
unsigned f;
switch(mm_a[0]) {
case 'r':
File = fopen(file_a, mm_a);
if (!File) {
menu_i = mn_ERR_4;
f = f2_ERR_4;
DEBUG(file_a, -1, (int)&G_mn[mn_ERR_4].s[10]);
}
break;
default:
if (!_disk_free(1))
File = NULL;
else
File = fopen(file_a, mm_a);
if (!File) {
menu_i = mn_ERR_2;
f = f2_ERR_2;
}
break;
}
if (!File) {
copy_window(P_i[i_WIN_VIEW], P_v[v_VIEW_0], 0x120, PEEK);
exchange_page();
copy_window(P_i[i_WIN_VIEW], P_v[v_VIEW_0], 0x120, POKE);
exchange_page();
disp_menu(menu_i, NULL, 13, 0);
res_bit(G_f2, f2_MOUSE);
set_bit(G_f2, f);
while (!inkey(ICN)) /* ICN ... skip write_replay() */
;
set_bit(G_f, f_ERR);
if (Temp)
fclose(Temp);
longjmp(G_jb, 0);
}
return(File);
}
void et_fwrite(void *buf_p, int type, int size, FILE *File)/*et*/
{
int i;
i = _disk_free(1);
if (i < type*size) {
if (i)
fwrite(buf_p, 1, i, File);
fclose(File);
set_err_1();
set_bit(G_f, f_ERR);
longjmp(G_jb, 0);
} else
fwrite(buf_p, type, size, File);
}
void set_err_1()/*et*/
{
draw_mode(1, mn_FREE_SAVE, L_WHT);
draw_mode(1, mn_FREE_WORK, L_WHT);
draw_free(1);
copy_window(P_i[i_WIN_VRAM], P_v[v_VRAM], 0x14, PEEK);
exchange_page();
copy_window(P_i[i_WIN_VRAM], P_v[v_VRAM], 0x14, POKE);
exchange_page();
disp_menu(mn_ERR_1, NULL, 13, 0);
res_bit(G_f2, f2_MOUSE);
set_bit(G_f2, f2_ERR_1);
while (!inkey(ICN)) /* ICN ... skip write_replay() */
;
}
int read_replay(int j)/*et*/
{
FILE *File;
int i;
G_nb.replay += j;
File = et_fopen(P_c[c_FILE_ET_TEMP], "rb", NULL);
fseek(File, G_nb.replay, 0);
fread(&i, sizeof(int), 1, File);
fclose(File);
G_nb.replay += sizeof(int);
return(i);
}
void write_replay(int key)/*et*/
{
FILE *File;
File = fopen(P_c[c_FILE_ET_TEMP], "rb");
if (File) {
fclose(File);
File = et_fopen(P_c[c_FILE_ET_TEMP], "ab", NULL);
} else
File = et_fopen(P_c[c_FILE_ET_TEMP], "wb", NULL);
et_fwrite(&key, sizeof(int), 1, File);
fclose(File);
G_nb.drive = *P_c[c_FILE_ET_TEMP];
}